import pymysql


class DBTools:
    @classmethod
    def __create_conn(cls):
        conn = pymysql.connect(host="211.103.136.244", port=7061, user="student", password="iHRM_student_2022", database="ihrm",
                               charset="utf8")
        return conn

    @classmethod
    def query_one(cls, sql):
        my_conn = None
        my_cursor = None
        res = None
        try:
            my_conn = DBTools.__create_conn()
            my_cursor = my_conn.cursor()
            my_cursor.execute(sql)
            res = my_cursor.fetchone()
        except Exception as err:
            print("调用查询异常：", str(err))
        finally:
            my_cursor.close()
            my_conn.close()
            return res

    @classmethod
    def db_uid(cls, sql):
        my_conn = None
        my_cursor = None
        res = None
        try:
            my_conn = DBTools.__create_conn()
            my_cursor = my_conn.cursor()
            my_cursor.execute(sql)
            print("执行结果：",my_conn.affected_rows())
            my_conn.commit()
        except Exception as err:
            print("调用查询异常：", str(err))
            my_conn.rollback()
        finally:
            my_cursor.close()
            my_conn.close()


if __name__ == '__main__':
    # result=DBTools.query_one("select * from t_hero;")
    # print(result)
    result = DBTools.db_uid("update t_hero set `read`=`read`+1 where name='任盈盈';")

